#include <gflags/gflags.h>

#include "coin2/base/string_util.h"
#include "coin2/exchange/base/order/sync_trade_client.h"
DEFINE_string(config_file_path, "config", "config file path");

using coin2::exchange::base::order::SyncTradeClient;
using coin2::exchange::order::OrderConnectionConfig;

std::string str = R"+*(
      {
        "mea": "Spot.Mexc.v3",
        "connection_config": {
          "key_filepath": "/home/qiangluo/config/mexc_trade_key.json"
        }
      }
      )+*";

void PrintCurrencyTransferElement(CurrencyTransferElement& element) {
  std::cout << "from_address: " << element.from_address() << "  ";
  std::cout << "to_address: " << element.to_address() << "  ";
  std::cout << "currency: " << element.currency() << "  ";
  std::cout << "qty: " << element.qty() << "  ";
  std::cout << "create_timestamp: " << element.create_timestamp() << "  ";
  std::cout << "status " << element.status() << "  ";
  if (element.type() == 1) {
    std::cout << "DEPOSIT";
  } else if (element.type() == 2) {
    std::cout << "WITHDRAW";
  } else if (element.type() == 3) {
    std::cout << "DEPOSIT_SUBACCOUNT";
  } else if (element.type() == 4) {
    std::cout << "WITHDRAW_SUBACCOUNT";
  }
  std::cout << " external transfer id " << element.external_transfer_id() << " transfer id "
            << element.transfer_id() << std::endl;
  std::cout << std::endl;
}

void test1(const std::string& config) {
  std::cout << config << std::endl;
  coin2::exchange::order::OrderSystemConfig osc_config;
  CHECK_THROW(JsonStringToProto(config, &osc_config));
  auto client = SyncTradeClient(osc_config);
  int a;
  while (1) {
    std::vector<coin::proto::CurrencyTransferElement> result;
    std::cout << "Enter the number of days to get:" << std::endl;
    std::cin >> a;
    client.QueryTransferHistories(GetLastNDayTimestamp(a), GetCurrentTimestamp(), &result);
    for (auto element : result) {
      PrintCurrencyTransferElement(element);
    }
    std::cout << "Get end" << std::endl;
  }
}

int main(int argc, char* argv[]) {
  // test();
  google::InitGoogleLogging(argv[0]);
  gflags::ParseCommandLineFlags(&argc, &argv, true);
  std::string path = FLAGS_config_file_path;
  std::string config = ReadFileContent(path);
  if (config.size() == 0) {
    config = str;
  }

  test1(config);
  return 0;
}

// bazel build -c dbg //experimental/qiang:trader_client_test -j 40